package com.tanhua.server.service;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.db.SettingApi;
import com.tanhua.server.utils.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingService {

    @Reference
    private QuestionApi questionApi;
    @Reference
    private SettingApi settingApi;

    @Reference
    private BlackListApi blackListApi;

//    获取用户的通用设置
    public ResponseEntity querySettings() {
        User user = UserHolder.getUser();
//        查询陌生人问题
        Question question = questionApi.findByUserId(user.getId());
//       查询这个人的通知设置
        Settings settings = settingApi.findByUserId(user.getId());
//        构建SettingVO对象
        SettingsVo settingsVo = new SettingsVo();

        settingsVo.setId(user.getId());
        settingsVo.setPhone(user.getMobile());
        if(question!=null){
            settingsVo.setStrangerQuestion(question.getTxt()); //但是 要注意空指针
        }
        if(settings!=null){
/*            settingsVo.setPinglunNotification(settings.getPinglunNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());*/
            BeanUtils.copyProperties(settings,settingsVo);
        }
        return ResponseEntity.ok(settingsVo);
    }

    public ResponseEntity saveQuestions(String content) {
//        考虑两种情况
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findByUserId(userId);
        if(question!=null){
//            1、当前登录人已经有陌生人问题
            question.setTxt(content);
            questionApi.update(question);
        }else{//                 2、当前登录人没有陌生人问题
            question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.save(question);
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity saveNotifications(Boolean like, Boolean pinglun, Boolean gonggao) {
//        判断当前登录人是否有通知设置
        Long userId = UserHolder.getUserId();
        Settings settings = settingApi.findByUserId(userId);
        if(settings!=null){
            settings.setGonggaoNotification(gonggao);
            settings.setPinglunNotification(pinglun);
            settings.setLikeNotification(like);
            settingApi.update(settings);
        }else{
            settings = new Settings();
            settings.setUserId(userId);
            settings.setGonggaoNotification(gonggao);
            settings.setPinglunNotification(pinglun);
            settings.setLikeNotification(like);
            settingApi.save(settings);
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity queryBlacklist(Integer page, Integer pagesize) {
//        查询某一个用户的黑名单  blacklist表
//        PageResult中的items放的应该是userInfo
      PageResult pageResult =  blackListApi.queryBlacklist(page,pagesize,UserHolder.getUserId());
      return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity deleteBlacklist(Long blackUserId) {
//        传入两个参数 userId blackUserId
        blackListApi.deleteBlacklist(UserHolder.getUserId(),blackUserId);
        return ResponseEntity.ok(null);
    }
}
